package ArrayPackage;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author Lzm
 * @version 1.0
 */
/*有序数组的平方有序数组*/
public class sortedSquares_ {
  @Test
  public void sortedSquaresTest(){
    int[] nums = new int[]{-4,-1,0,3,10};
    int[] ints = sortedSquares(nums);
    System.out.println(Arrays.toString(ints));
  }

  public int[] sortedSquares(int[] nums){
    int[] result = new int[nums.length];
    if (nums.length <= 0 ){
      return result;
    }
    int left = 0;
    int right = nums.length - 1;
    int index = nums.length - 1;
    while(left <= right){
      if (nums[left] * nums[left] < nums[right] * nums[right]){
        result[index--] = nums[right] * nums[right];
        right--;
      }
      else{
        result[index--] = nums[left] * nums[left];
        left++;
      }
    }
    return result;
  }
}
